﻿package fui.FUIComponents{
	import flash.events.*;
	import flash.display.*;
	import fl.controls.*;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import fl.containers.ScrollPane;
	import flash.events.EventDispatcher;
	import flash.system.Security;
	
	import fui.FUIControllers.FUIMenuEvent;
	
	public class FUIMenu extends Sprite {
		private var _options:Array;
		private var btns:Array;
		public function FUIMenu(activeOptions:Array){
			this._options = activeOptions;
			listOption();
			buildBtns();
		}
		private function listOption():void
		{
			var i:int;
			for(i = 0; i< this._options.length; i++)
			{
				trace(this._options[i]);
			}
		}
		public function rebuildBtns():void
		{
			for (var i:int = 0; i < numChildren; i++) 
			{
    				removeChild(getChildAt(i));
    				i--; // because now the child list has decreased by one, aka the clip you just removed 
			}
			buildBtns();
			
		}
		private function buildBtns():void
		{
			btns = new Array();
			var i:int;
			trace("build buttons");
			for(i = 0; i < _options.length; i++)
			{
				var tmp:Button = new Button();
				tmp.y = y  + (22*i);
				tmp.x = x;
				tmp.setSize(150,20);
				trace(_options[i]);
				tmp.label = _options[i];
				tmp.name =  _options[i];
				btns.push(tmp);
				tmp.addEventListener(MouseEvent.CLICK, onBtnClick);
				addChild(tmp);
			}
				
		}
		public function onBtnClick(event:Event):void
		{
			
			var menuevent:FUIMenuEvent = new FUIMenuEvent("onMenuClick");
			menuevent.setContent(event.target.name);
			dispatchEvent(menuevent);
			
			var i:int;
			for (i = 0; i< _options.length; i++)
			{
				if(_options[i] == event.target.name)
				{
					trace("choosen:" + _options[i]);
				}
			}
			
		}
	}
}